
const app = new Vue({
    el: '#app',
    data: {
        books: [
            {
                id:1,
                name: '《算法导论》',
                data: '2006-9',
                price: 80.00,
                count: 1
            },
            {
                id:2,
                name: '《UNIX》',
                data: '2006-9',
                price: 70.00,
                count: 1
            },
            {
                id:3,
                name: '《编程》',
                data: '2006-9',
                price: 30.00,
                count: 1
            },
            {
                id:4,
                name: '《js忍者秘籍》',
                data: '2006-9',
                price: 69.00,
                count: 1
            },
            {
                id:5,
                name: '《数据结构》',
                data: '2006-9',
                price: 40.00,
                count: 1
            }
            ]
    },
    methods: {
        getFinalPrice(price){
            return '￥' + price.toFixed(2);
        },
        increment(index){
            return  this.books[index].count++;
        },
        decrement(index) {
            return  this.books[index].count--;
        },
        remove(index){
            return this.books.splice(index,1);
        },
    },
    computed: {
        totalPrice() {
            let totalPrice =0;
            for (let i = 0; i < this.books.length; i++){
                totalPrice += this.books[i].price * this.books[i].count
            }
            return totalPrice;
        }
    },
    //过滤器，一般情况下是函数，会把过滤的东西做参数传过来
    filters: {
        showPrice(price) {
            return '￥' + price.toFixed(2);
        }
    }
})